package src.com.foreknow.demo21;

import src.com.foreknow.demo12.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {

    public void m1(List<User>list){
        for (int i = 0;i<list.size();i++){
            User user = list.get(i);
            System.out.println(user.getName());
        }
    }

    public List<String> m2(){
        List<String> list = new ArrayList<>();
        list.add("fender");
        list.add("gibson");
        list.add("jack");
        list.add("fender");
        return list;
    }
    public static void main(String[] args) {
        //1.创建一个集合容器List 如何约束集合中元素的类型？可以使用范型<E>
        List<String> list = new ArrayList<>();
        //2.向List中添加元素
        list.add("fender");
        list.add("gibson");
        list.add("jack");
        list.add("fender");
        //3.获取到集合中的某一个元素
        System.out.println("集合中元素的个数："+ list.size());
        list.get(2);
        System.out.println("-------------------------------");
        //4.遍历List集合
        for (int i = 0;i<list.size();i++){
            String str = list.get(i);
            System.out.println(str);
        }
        System.out.println("-----------foreach-----------------");
        for (String str:list) {
            System.out.println(str);
        }

        List<User> list1 = new ArrayList<>();
        User u1 = new User();
        u1.setName("fender");
        User u2 = new User();
        u2.setName("gibson");
        User u3= new User();
        u3.setName("yamaha");
        list1.add(u1);
        list1.add(u2);
        list1.add(u3);
        for (User user:list1) {
            System.out.println(user.getName());
        }
        System.out.println("-----------迭代器Iterator接口--------------");
        Iterator<User> it = list1.iterator();
        while(it.hasNext()){
            User user = it.next();
            System.out.println(user.getName());
        }
        System.out.println("--------------------------------");
        ListDemo listDemo = new ListDemo();
        listDemo.m1(list1);
        List<String> userList = listDemo.m2();
        for (String str:userList) {
            System.out.println(str);

        }



    }
}












